import { CloudDBZoneWrapper } from '../clouddb/CloudDBZoneWrapper.js';
import * as Utils from '../utils/Utils';

const TAG: string = "cloudDB-->"

// event: {"id":3,"bornYear":2020,"bornName":"鼠"}
export const myHandler = async function (event, context, callback, logger) {
  const credential = Utils.getCredential(context, logger);

  try {
    const cloudDBZoneWrapper = new CloudDBZoneWrapper(credential, logger);
    let bornInfo = cloudDBZoneWrapper.getInfo(event.id);
    let result = await cloudDBZoneWrapper.updateRecord(bornInfo, event);
    if (result) {
      callback({
        result: { code: 0, desc: "SUCCESS" }
      });
    } else {
      callback({
        result: { code: -1, desc: "FAILED" }
      });
    }
  } catch (err) {
    logger.error(TAG + "func error:" + err.message + " stack:" + err.stack);
    callback({
      result: { code: -1, desc: "ERROR" },
    });
  }
};